/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.Controller');

//TODO: depricated?
goog.require('kramerius.audio.Playlist');

/** @const */ var MIN_VOLUME = 0;
/** @const */ var MAX_VOLUME = 100;
/** @const */ var DEFAULT_VOLUME = 50;

/**
 * @constructor 
 * @param {boolean | undefined} debugMode Whether events should be logged.
 */
kramerius.audio.Controller = function(debugMode){
    /** @private **/
    this.audioPlayer_ = undefined;
    /** @private **/
    this.playList_ = undefined;
    
    /** @private **/
    this.loggingOn_ = debugMode && true;
    if (this.loggingOn_){
        console.log("Controller: initializing (logging: " + this.loggingOn_ + ")" );
    }
    this.volume_ = DEFAULT_VOLUME;
};


kramerius.audio.Controller.prototype.setPosition = function(position){
    this.audioPlayer_.setPosition(position);
    if (this.progressBar_ != undefined){
        this.progressBar_.setPosition(position);
    }
    if (this.progressInfo_ != undefined){
        this.progressInfo_.setPosition(position/1000);
    }
}


kramerius.audio.Controller.prototype.setTotalLength = function(length){
    if (this.progressBar_ != undefined){
        this.progressBar_.setTotalLength(length);
    }
    if (this.progressInfo_ != undefined){
        this.progressInfo_.setTotalLength(length/1000);
    }
};


kramerius.audio.Controller.prototype.setAudioPlayer = function(audioPlayer){
    this.audioPlayer_ = audioPlayer;
};


kramerius.audio.Controller.prototype.setTrack = function(track){
    this.audioPlayer_.setTrack(track);
    console.log("controller: setting track " + track);
};


kramerius.audio.Controller.prototype.play = function(){
    if (this.loggingOn_){
        console.log("Controller: playing");
    }
    this.audioPlayer_.play();
};

kramerius.audio.Controller.prototype.playNextTrackFromPlaylist = function(){
    var nextItem = this.playlist_.getNextItemToPlay();
    if (nextItem != null){
        this.playlist_.playItem(nextItem);
    }
}

kramerius.audio.Controller.prototype.setPlayerState = function(state){
    console.log("Controller: setting player state to " + state);
    switch(state){
        case 'empty':
            this.playPauseButton_.setPlayDisabledImage();
            this.stopButton_.setDisabledImage();
            this.progressBar_.disable();
            this.setVisualPosition(0);
            break;
        case 'stopped':
            this.playPauseButton_.setPlayImage();
            this.stopButton_.setDisabledImage();
            this.progressBar_.enable();
            this.setPosition(0);
            break;
        case 'playing':
            this.playPauseButton_.setPauseImage();
            this.stopButton_.setEnabledImage();
            this.progressBar_.enable();
            break;
        case 'paused':
            this.playPauseButton_.setPlayImage();
            this.stopButton_.setEnabledImage();
            this.progressBar_.enable();
            break;
    }
};


kramerius.audio.Controller.prototype.pause = function(){
    if (this.loggingOn_){
        console.log("Controller: pausing");
    }
    if (!this.audioPlayer_.isPlayingOrBuffering()){
        console.log("Controller: WARNING: cannot pause: not playing");    
    }
    this.audioPlayer_.pause();
}

kramerius.audio.Controller.prototype.resume = function(){
    this.audioPlayer_.resume();
    if (this.loggingOn_){
        console.log("Controller: resuming");
    }
    if(!!this.audioPlayer_.isPaused()){
        console.log("Controller: WARNING: cannot resume: not paused");    
    }
    this.audioPlayer_.resume();
}


kramerius.audio.Controller.prototype.stop = function(){
    if (this.loggingOn_){
        console.log("Controller: stopping");
    }
    this.audioPlayer_.stop();
}

kramerius.audio.Controller.prototype.setVolume = function(volume){
    if (volume < MIN_VOLUME || volume > MAX_VOLUME){
        console.log("Controller: WARNING: Illegal volume level " + volume + ". Ignoring.")
    }else{
        this.volume_ = volume;
        if (this.audioPlayer_ != undefined){
            this.audioPlayer_.setVolume(volume);
        }
        if (this.loggingOn_){
          //  console.log("Controller: volume level=" + volume);    
        }
    }
}


kramerius.audio.Controller.prototype.mute = function(){
    if (this.loggingOn_){
        console.log("Controller: muting");
    }
    this.audioPlayer_.setVolume(0);
    this.mute_ = true;
    this.volumeButton_.setDisabledImage();
    this.volumeSlider_.disable();
}

kramerius.audio.Controller.prototype.unMute = function(){
    this.mute_ = false;
    this.audioPlayer_.setVolume(this.volume_);
    this.volumeButton_.setEnabledImage();
    this.volumeSlider_.enable();
}

kramerius.audio.Controller.prototype.setPlayPauseButton = function(button){
    this.playPauseButton_ = button;
}

kramerius.audio.Controller.prototype.setStopButton = function(button){
    this.stopButton_ = button;
}

kramerius.audio.Controller.prototype.setVolumeButton = function(button){
    this.volumeButton_ = button;
}

kramerius.audio.Controller.prototype.setProgressBar = function(progressBar){
    this.progressBar_ = progressBar;
}

kramerius.audio.Controller.prototype.setProgressInfo = function(progressInfo){
    this.progressInfo_ = progressInfo;
}


kramerius.audio.Controller.prototype.setVolumeSlider = function(volumeSlider){
    this.volumeSlider_ = volumeSlider;
}


kramerius.audio.Controller.prototype.setPlaylistShowHideButton = function(playlistShowHideButton){
    this.playlistShowHideButton_ = playlistShowHideButton;
}

kramerius.audio.Controller.prototype.setPlaylist = function(playlist){
    this.playlist_ = playlist;
}

kramerius.audio.Controller.prototype.getVolume = function(){
    return this.volume_;
}



